import 'package:simulator/core/module.dart';
import 'package:simulator/core/op_code.dart';
import 'package:simulator/core/ram_rom.dart';
import 'package:simulator/core/regisiter.dart';
import 'package:simulator/core/simulator.dart';
import 'package:simulator/hardware/mcs51/cpu_status.dart';
import 'package:simulator/utils/utils.dart';
import 'package:win32/win32.dart';

part '_mcs51.dart';

const ajmpOp = OpCode(
    code: 0x01, label: 'AJMP addr11', data: 1, time: 2, callback: _ajmpAddr11);
const acallOp = OpCode(
    code: 0x11,
    label: 'ACALL addr11',
    data: 1,
    time: 2,
    callback: _acallAddr11);

OpCode? mcs51RestOpCode(int opcode) {
  opcode &= 0x1f;
  if (opcode == 0x11) {
    return acallOp;
  } else if (opcode == 0x01) {
    return ajmpOp;
  }
  return null;
}

///指令集
List<OpCode> opMCS51Code = const [
  OpCode(code: 0, label: 'NOP', data: 0, time: 1, callback: _nop),
  ajmpOp,
  OpCode(
      code: 0x02,
      label: 'LJMP addr16',
      data: 2,
      time: 2,
      callback: _ljmpAddr16),
  OpCode(code: 0x03, label: 'RR A', data: 0, time: 1, callback: _rrA),
  OpCode(code: 0x04, label: 'INC A', data: 0, time: 1, callback: _incA),
  OpCode(
      code: 0x05, label: 'INC direct', data: 1, time: 1, callback: _incDirect),
  OpCode(code: 0x06, label: 'INC @R0', data: 0, time: 1, callback: _incFromR0),
  OpCode(code: 0x07, label: 'INC @R1', data: 0, time: 1, callback: _incFromR1),
  OpCode(code: 0x08, label: 'INC R0', data: 0, time: 1, callback: _incR0),
  OpCode(code: 0x09, label: 'INC R1', data: 0, time: 1, callback: _incR1),
  OpCode(code: 0x0a, label: 'INC R2', data: 0, time: 1, callback: _incR2),
  OpCode(code: 0x0b, label: 'INC R3', data: 0, time: 1, callback: _incR3),
  OpCode(code: 0x0c, label: 'INC R4', data: 0, time: 1, callback: _incR4),
  OpCode(code: 0x0d, label: 'INC R5', data: 0, time: 1, callback: _incR5),
  OpCode(code: 0x0e, label: 'INC R6', data: 0, time: 1, callback: _incR6),
  OpCode(code: 0x0f, label: 'INC R7', data: 0, time: 1, callback: _incR7),
  OpCode(code: 0x10, label: 'JBC REL', data: 2, time: 2, callback: _jbcREL),
  acallOp,
  OpCode(
      code: 0x12,
      label: 'LCALL addr16',
      data: 2,
      time: 2,
      callback: _lcallAddr16),
  OpCode(code: 0x13, label: 'RRC A', data: 0, time: 1, callback: _rrcA),
  OpCode(code: 0x14, label: 'DEC A', data: 0, time: 1, callback: _decA),
  OpCode(
      code: 0x15, label: 'DEC direct', data: 1, time: 1, callback: _decDirect),
  OpCode(code: 0x16, label: 'DEC @R0', data: 0, time: 1, callback: _decFromR0),
  OpCode(code: 0x17, label: 'DEC @R1', data: 0, time: 1, callback: _decFromR1),
  OpCode(code: 0x18, label: 'DEC R0', data: 0, time: 2, callback: _decR0),
  OpCode(code: 0x19, label: 'DEC R1', data: 0, time: 2, callback: _decR1),
  OpCode(code: 0x1a, label: 'DEC R2', data: 0, time: 2, callback: _decR2),
  OpCode(code: 0x1b, label: 'DEC R3', data: 0, time: 2, callback: _decR3),
  OpCode(code: 0x1c, label: 'DEC R4', data: 0, time: 2, callback: _decR4),
  OpCode(code: 0x1d, label: 'DEC R5', data: 0, time: 2, callback: _decR5),
  OpCode(code: 0x1e, label: 'DEC R6', data: 0, time: 2, callback: _decR6),
  OpCode(code: 0x1f, label: 'DEC R7', data: 0, time: 2, callback: _decR7),
  OpCode(code: 0x20, label: 'JB REL', data: 2, time: 2, callback: _jbREL),
  OpCode(code: 0x22, label: 'RET', data: 0, time: 2, callback: _ret),
  OpCode(code: 0x23, label: 'RL A', data: 0, time: 1, callback: _rlA),
  OpCode(
      code: 0x24, label: 'ADD A,#data', data: 1, time: 1, callback: _addAData),
  OpCode(
      code: 0x25,
      label: 'ADD A,direct',
      data: 1,
      time: 1,
      callback: _addADirect),
  OpCode(
      code: 0x26, label: 'ADD A,@R0', data: 0, time: 1, callback: _addAFromR0),
  OpCode(
      code: 0x27, label: 'ADD A,@R1', data: 0, time: 1, callback: _addAFromR1),
  OpCode(code: 0x28, label: 'ADD A,R0', data: 0, time: 1, callback: _addAR0),
  OpCode(code: 0x29, label: 'ADD A,R1', data: 0, time: 1, callback: _addAR1),
  OpCode(code: 0x2a, label: 'ADD A,R2', data: 0, time: 1, callback: _addAR2),
  OpCode(code: 0x2b, label: 'ADD A,R3', data: 0, time: 1, callback: _addAR3),
  OpCode(code: 0x2c, label: 'ADD A,R4', data: 0, time: 1, callback: _addAR4),
  OpCode(code: 0x2d, label: 'ADD A,R5', data: 0, time: 1, callback: _addAR5),
  OpCode(code: 0x2e, label: 'ADD A,R6', data: 0, time: 1, callback: _addAR6),
  OpCode(code: 0x2f, label: 'ADD A,R7', data: 0, time: 1, callback: _addAR7),
  OpCode(code: 0x30, label: 'JNB REL', data: 2, time: 2, callback: _jnbREL),
  OpCode(code: 0x32, label: 'RETI', data: 0, time: 2, callback: _reti),
  OpCode(code: 0x33, label: 'RLC A', data: 0, time: 1, callback: _rlcA),
  OpCode(
      code: 0x34,
      label: 'ADDC A,#DATA',
      data: 1,
      time: 1,
      callback: _addcAData),
  OpCode(
      code: 0x35,
      label: 'ADDC A,direct',
      data: 1,
      time: 1,
      callback: _addcADirect),
  OpCode(
      code: 0x36,
      label: 'ADDC A,@R0',
      data: 0,
      time: 1,
      callback: _addcAFromR0),
  OpCode(
      code: 0x37,
      label: 'ADDC A,@R1',
      data: 0,
      time: 1,
      callback: _addcAFromR1),
  OpCode(code: 0x38, label: 'ADDC A,R0', data: 0, time: 1, callback: _addcAR0),
  OpCode(code: 0x39, label: 'ADDC A,R1', data: 0, time: 1, callback: _addcAR1),
  OpCode(code: 0x3a, label: 'ADDC A,R2', data: 0, time: 1, callback: _addcAR2),
  OpCode(code: 0x3b, label: 'ADDC A,R3', data: 0, time: 1, callback: _addcAR3),
  OpCode(code: 0x3c, label: 'ADDC A,R4', data: 0, time: 1, callback: _addcAR4),
  OpCode(code: 0x3d, label: 'ADDC A,R5', data: 0, time: 1, callback: _addcAR5),
  OpCode(code: 0x3e, label: 'ADDC A,R6', data: 0, time: 1, callback: _addcAR6),
  OpCode(code: 0x3f, label: 'ADDC A,R7', data: 0, time: 1, callback: _addcAR7),
  OpCode(code: 0x40, label: 'JC REL', data: 1, time: 2, callback: _jcREL),
  OpCode(
      code: 0x42,
      label: 'ORL direct,A',
      data: 1,
      time: 1,
      callback: _orlDirectA),
  OpCode(
      code: 0x43,
      label: 'ORL direct,#DATA',
      data: 2,
      time: 2,
      callback: _orlDirectData),
  OpCode(
      code: 0x44, label: 'ORL A,#DATA', data: 1, time: 1, callback: _orlAData),
  OpCode(
      code: 0x45,
      label: 'ORL A,direct',
      data: 1,
      time: 1,
      callback: _orlADirect),
  OpCode(
      code: 0x46, label: 'ORL A,@R0', data: 0, time: 1, callback: _orlAFromR0),
  OpCode(
      code: 0x47, label: 'ORL A,@R1', data: 0, time: 1, callback: _orlAFromR1),
  OpCode(code: 0x48, label: 'ORL A,R0', data: 0, time: 1, callback: _orlAR0),
  OpCode(code: 0x49, label: 'ORL A,R1', data: 0, time: 1, callback: _orlAR1),
  OpCode(code: 0x4a, label: 'ORL A,R2', data: 0, time: 1, callback: _orlAR2),
  OpCode(code: 0x4b, label: 'ORL A,R3', data: 0, time: 1, callback: _orlAR3),
  OpCode(code: 0x4c, label: 'ORL A,R4', data: 0, time: 1, callback: _orlAR4),
  OpCode(code: 0x4d, label: 'ORL A,R5', data: 0, time: 1, callback: _orlAR5),
  OpCode(code: 0x4e, label: 'ORL A,R6', data: 0, time: 1, callback: _orlAR6),
  OpCode(code: 0x4f, label: 'ORL A,R7', data: 0, time: 1, callback: _orlAR7),
  OpCode(code: 0x50, label: 'JNC REL', data: 1, time: 2, callback: _jncREL),
  OpCode(
      code: 0x52,
      label: 'ANL direct,A',
      data: 1,
      time: 1,
      callback: _anlDirectA),
  OpCode(
      code: 0x53,
      label: 'ANL direct,#DATA',
      data: 2,
      time: 2,
      callback: _anlDirectData),
  OpCode(
      code: 0x54, label: 'ANL A,#DATA', data: 1, time: 1, callback: _anlAData),
  OpCode(
      code: 0x55,
      label: 'ANL A,direct',
      data: 1,
      time: 1,
      callback: _anlADirect),
  OpCode(
      code: 0x56, label: 'ANL A,@R0', data: 0, time: 1, callback: _anlAFromR0),
  OpCode(
      code: 0x57, label: 'ANL A,@R1', data: 0, time: 1, callback: _anlAFromR1),
  OpCode(code: 0x58, label: 'ANL A,R0', data: 0, time: 1, callback: _anlAR0),
  OpCode(code: 0x59, label: 'ANL A,R1', data: 0, time: 1, callback: _anlAR1),
  OpCode(code: 0x5a, label: 'ANL A,R2', data: 0, time: 1, callback: _anlAR2),
  OpCode(code: 0x5b, label: 'ANL A,R3', data: 0, time: 1, callback: _anlAR3),
  OpCode(code: 0x5c, label: 'ANL A,R4', data: 0, time: 1, callback: _anlAR4),
  OpCode(code: 0x5d, label: 'ANL A,R5', data: 0, time: 1, callback: _anlAR5),
  OpCode(code: 0x5e, label: 'ANL A,R6', data: 0, time: 1, callback: _anlAR6),
  OpCode(code: 0x5f, label: 'ANL A,R7', data: 0, time: 1, callback: _anlAR7),
  OpCode(code: 0x60, label: 'JZ REL', data: 1, time: 2, callback: _jzREL),
  OpCode(
      code: 0x62,
      label: 'XRL direct,A',
      data: 1,
      time: 1,
      callback: _xrlDirectA),
  OpCode(
      code: 0x63,
      label: 'XRL direct,#DATA',
      data: 2,
      time: 2,
      callback: _xrlDirectData),
  OpCode(
      code: 0x64, label: 'XRL A,#DATA', data: 1, time: 1, callback: _xrlAData),
  OpCode(
      code: 0x65,
      label: 'XRL A,direct',
      data: 1,
      time: 1,
      callback: _xrlADirect),
  OpCode(
      code: 0x66, label: 'XRL A,@R0', data: 0, time: 1, callback: _xrlAFromR0),
  OpCode(
      code: 0x67, label: 'XRL A,@R1', data: 0, time: 1, callback: _xrlAFromR1),
  OpCode(code: 0x68, label: 'XRL A,R0', data: 0, time: 1, callback: _xrlAR0),
  OpCode(code: 0x69, label: 'XRL A,R1', data: 0, time: 1, callback: _xrlAR1),
  OpCode(code: 0x6a, label: 'XRL A,R2', data: 0, time: 1, callback: _xrlAR2),
  OpCode(code: 0x6b, label: 'XRL A,R3', data: 0, time: 1, callback: _xrlAR3),
  OpCode(code: 0x6c, label: 'XRL A,R4', data: 0, time: 1, callback: _xrlAR4),
  OpCode(code: 0x6d, label: 'XRL A,R5', data: 0, time: 1, callback: _xrlAR5),
  OpCode(code: 0x6e, label: 'XRL A,R6', data: 0, time: 1, callback: _xrlAR6),
  OpCode(code: 0x6f, label: 'XRL A,R7', data: 0, time: 1, callback: _xrlAR7),
  OpCode(code: 0x70, label: 'JNZ REL', data: 1, time: 2, callback: _jnzREL),
  OpCode(code: 0x72, label: 'ORL C,bit', data: 1, time: 2, callback: _orlCBit),
  OpCode(
      code: 0x73,
      label: 'JMP @A+DPTR',
      data: 0,
      time: 2,
      callback: _jmpFromADPTR),
  OpCode(
      code: 0x74, label: 'MOV A,#data', data: 1, time: 1, callback: _moveAData),
  OpCode(
      code: 0x75,
      label: 'MOV direct,#data',
      data: 2,
      time: 2,
      callback: _moveDirectData),
  OpCode(
      code: 0x76,
      label: 'MOV @R0,#data',
      data: 1,
      time: 1,
      callback: _moveFormR0Data),
  OpCode(
      code: 0x77,
      label: 'MOV @R1,#data',
      data: 1,
      time: 1,
      callback: _moveFormR1Data),
  OpCode(
      code: 0x78,
      label: 'MOV R0,#data',
      data: 1,
      time: 1,
      callback: _moveR0Data),
  OpCode(
      code: 0x79,
      label: 'MOV R1,#data',
      data: 1,
      time: 1,
      callback: _moveR1Data),
  OpCode(
      code: 0x7a,
      label: 'MOV R2,#data',
      data: 1,
      time: 1,
      callback: _moveR2Data),
  OpCode(
      code: 0x7b,
      label: 'MOV R3,#data',
      data: 1,
      time: 1,
      callback: _moveR3Data),
  OpCode(
      code: 0x7c,
      label: 'MOV R4,#data',
      data: 1,
      time: 1,
      callback: _moveR4Data),
  OpCode(
      code: 0x7d,
      label: 'MOV R5,#data',
      data: 1,
      time: 1,
      callback: _moveR5Data),
  OpCode(
      code: 0x7e,
      label: 'MOV R6,#data',
      data: 1,
      time: 1,
      callback: _moveR6Data),
  OpCode(
      code: 0x7f,
      label: 'MOV R7,#data',
      data: 1,
      time: 1,
      callback: _moveR7Data),
  OpCode(code: 0x80, label: 'SJMP REL', data: 1, time: 2, callback: _sjmpRel),
  OpCode(code: 0x82, label: 'ANL C,bit', data: 1, time: 2, callback: _anlCBit),
  OpCode(
      code: 0x83,
      label: 'MOVC A,@A+PC',
      data: 0,
      time: 2,
      callback: _movecAAPC),
  OpCode(code: 0x84, label: 'DIV AB', data: 0, time: 4, callback: _divAB),
  OpCode(
      code: 0x85,
      label: 'MOV direct,direct',
      data: 2,
      time: 2,
      callback: _moveDirectDirect),
  OpCode(
      code: 0x86,
      label: 'MOV direct,@R0',
      data: 1,
      time: 2,
      callback: _moveDirectFromR0),
  OpCode(
      code: 0x87,
      label: 'MOV direct,@R1',
      data: 1,
      time: 2,
      callback: _moveDirectFromR1),
  OpCode(
      code: 0x88,
      label: 'MOV direct,R0',
      data: 1,
      time: 2,
      callback: _moveDirectR0),
  OpCode(
      code: 0x89,
      label: 'MOV direct,R1',
      data: 1,
      time: 2,
      callback: _moveDirectR1),
  OpCode(
      code: 0x8a,
      label: 'MOV direct,R2',
      data: 1,
      time: 2,
      callback: _moveDirectR2),
  OpCode(
      code: 0x8b,
      label: 'MOV direct,R3',
      data: 1,
      time: 2,
      callback: _moveDirectR3),
  OpCode(
      code: 0x8c,
      label: 'MOV direct,R4',
      data: 1,
      time: 2,
      callback: _moveDirectR4),
  OpCode(
      code: 0x8d,
      label: 'MOV direct,R5',
      data: 1,
      time: 2,
      callback: _moveDirectR5),
  OpCode(
      code: 0x8e,
      label: 'MOV direct,R6',
      data: 1,
      time: 2,
      callback: _moveDirectR6),
  OpCode(
      code: 0x8f,
      label: 'MOV direct,R7',
      data: 1,
      time: 2,
      callback: _moveDirectR7),
  OpCode(
      code: 0x90,
      label: 'MOV DPTR,#data16',
      data: 2,
      time: 2,
      callback: _moveDptrData16),
  OpCode(code: 0x92, label: 'MOV bit,C', data: 1, time: 2, callback: _moveBitC),
  OpCode(
      code: 0x93,
      label: 'MOVC A,@A+DPTR',
      data: 0,
      time: 2,
      callback: _movecAADptr),
  OpCode(
      code: 0x94,
      label: 'SUBB A,#DATA',
      data: 1,
      time: 1,
      callback: _subbAData),
  OpCode(
      code: 0x95,
      label: 'SUBB A,direct',
      data: 1,
      time: 1,
      callback: _subbADirect),
  OpCode(
      code: 0x96,
      label: 'SUBB A,@R0',
      data: 0,
      time: 1,
      callback: _subbAFromR0),
  OpCode(
      code: 0x97,
      label: 'SUBB A,@R1',
      data: 0,
      time: 1,
      callback: _subbAFromR1),
  OpCode(code: 0x98, label: 'SUBB A,R0', data: 0, time: 1, callback: _subbAR0),
  OpCode(code: 0x99, label: 'SUBB A,R1', data: 0, time: 1, callback: _subbAR1),
  OpCode(code: 0x9a, label: 'SUBB A,R2', data: 0, time: 1, callback: _subbAR2),
  OpCode(code: 0x9b, label: 'SUBB A,R3', data: 0, time: 1, callback: _subbAR3),
  OpCode(code: 0x9c, label: 'SUBB A,R4', data: 0, time: 1, callback: _subbAR4),
  OpCode(code: 0x9d, label: 'SUBB A,R5', data: 0, time: 1, callback: _subbAR5),
  OpCode(code: 0x9e, label: 'SUBB A,R6', data: 0, time: 1, callback: _subbAR6),
  OpCode(code: 0x9f, label: 'SUBB A,R7', data: 0, time: 1, callback: _subbAR7),
  OpCode(
      code: 0xa0, label: 'ORL C,/bit', data: 1, time: 2, callback: _orlCFBit),
  OpCode(code: 0xa2, label: 'MOV C,bit', data: 1, time: 2, callback: _moveCBit),
  OpCode(code: 0xa3, label: 'INC DPTR', data: 0, time: 2, callback: _incDptr),
  OpCode(code: 0xa4, label: 'MUL AB', data: 0, time: 4, callback: _mulAB),
  OpCode(
      code: 0xa6,
      label: 'MOV @R0,direct',
      data: 1,
      time: 2,
      callback: _moveFormR0Direct),
  OpCode(
      code: 0xa7,
      label: 'MOV @R1,direct',
      data: 1,
      time: 2,
      callback: _moveFormR1Direct),
  OpCode(
      code: 0xa8,
      label: 'MOV R0,direct',
      data: 1,
      time: 2,
      callback: _moveR0Direct),
  OpCode(
      code: 0xa9,
      label: 'MOV R1,direct',
      data: 1,
      time: 2,
      callback: _moveR1Direct),
  OpCode(
      code: 0xaa,
      label: 'MOV R2,direct',
      data: 1,
      time: 2,
      callback: _moveR2Direct),
  OpCode(
      code: 0xab,
      label: 'MOV R3,direct',
      data: 1,
      time: 2,
      callback: _moveR3Direct),
  OpCode(
      code: 0xac,
      label: 'MOV R4,direct',
      data: 1,
      time: 2,
      callback: _moveR4Direct),
  OpCode(
      code: 0xad,
      label: 'MOV R5,direct',
      data: 1,
      time: 2,
      callback: _moveR5Direct),
  OpCode(
      code: 0xae,
      label: 'MOV R6,direct',
      data: 1,
      time: 2,
      callback: _moveR6Direct),
  OpCode(
      code: 0xaf,
      label: 'MOV R7,direct',
      data: 1,
      time: 2,
      callback: _moveR7Direct),
  OpCode(
      code: 0xb0, label: 'ANL C,/bit', data: 1, time: 2, callback: _anlCFBit),
  OpCode(code: 0xb2, label: 'CPL bit', data: 1, time: 1, callback: _cplBit),
  OpCode(code: 0xb3, label: 'CPL C', data: 0, time: 1, callback: _cplC),
  OpCode(
      code: 0xb4,
      label: 'CJNE A,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneADataREL),
  OpCode(
      code: 0xb5,
      label: 'CJNE A,direct,REL',
      data: 2,
      time: 2,
      callback: _cjneADirectREL),
  OpCode(
      code: 0xb6,
      label: 'CJNE @R0,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneFromR0DataREL),
  OpCode(
      code: 0xb7,
      label: 'CJNE @R1,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneFromR1DataREL),
  OpCode(
      code: 0xb8,
      label: 'CJNE R0,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR0DataREL),
  OpCode(
      code: 0xb9,
      label: 'CJNE R1,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR1DataREL),
  OpCode(
      code: 0xba,
      label: 'CJNE R2,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR2DataREL),
  OpCode(
      code: 0xbb,
      label: 'CJNE R3,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR3DataREL),
  OpCode(
      code: 0xbc,
      label: 'CJNE R4,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR4DataREL),
  OpCode(
      code: 0xbd,
      label: 'CJNE R5,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR5DataREL),
  OpCode(
      code: 0xbe,
      label: 'CJNE R6,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR6DataREL),
  OpCode(
      code: 0xbf,
      label: 'CJNE R7,#DATA,REL',
      data: 2,
      time: 2,
      callback: _cjneR7DataREL),
  OpCode(
      code: 0xc0,
      label: 'PUSH direct',
      data: 1,
      time: 2,
      callback: _pushDirect),
  OpCode(code: 0xc2, label: 'CLR bit', data: 1, time: 1, callback: _clrBit),
  OpCode(code: 0xc3, label: 'CLR C', data: 0, time: 1, callback: _clrC),
  OpCode(code: 0xc4, label: 'SWAP A', data: 0, time: 1, callback: _swapA),
  OpCode(
      code: 0xc5,
      label: 'XCH A,direct',
      data: 1,
      time: 1,
      callback: _xchADirect),
  OpCode(
      code: 0xc6, label: 'XCH A,@R0', data: 0, time: 1, callback: _xchAFromR0),
  OpCode(
      code: 0xc7, label: 'XCH A,@R1', data: 0, time: 1, callback: _xchAFromR1),
  OpCode(code: 0xc8, label: 'XCH A,R0', data: 0, time: 1, callback: _xchAR0),
  OpCode(code: 0xc9, label: 'XCH A,R1', data: 0, time: 1, callback: _xchAR1),
  OpCode(code: 0xca, label: 'XCH A,R2', data: 0, time: 1, callback: _xchAR2),
  OpCode(code: 0xcb, label: 'XCH A,R3', data: 0, time: 1, callback: _xchAR3),
  OpCode(code: 0xcc, label: 'XCH A,R4', data: 0, time: 1, callback: _xchAR4),
  OpCode(code: 0xcd, label: 'XCH A,R5', data: 0, time: 1, callback: _xchAR5),
  OpCode(code: 0xce, label: 'XCH A,R6', data: 0, time: 1, callback: _xchAR6),
  OpCode(code: 0xcf, label: 'XCH A,R7', data: 0, time: 1, callback: _xchAR7),
  OpCode(
      code: 0xd0, label: 'POP direct', data: 1, time: 2, callback: _popDirect),
  OpCode(code: 0xd2, label: 'SETB bit', data: 1, time: 1, callback: _setbBit),
  OpCode(code: 0xd3, label: 'SETB C', data: 0, time: 1, callback: _setbC),
  OpCode(code: 0xd4, label: 'DA A', data: 0, time: 1, callback: _daa),
  OpCode(
      code: 0xd5,
      label: 'DJNZ direct,REL',
      data: 2,
      time: 2,
      callback: _djnzDirectREL),
  OpCode(
      code: 0xd6,
      label: 'XCHD A,@R0',
      data: 0,
      time: 1,
      callback: _xchdAFromR0),
  OpCode(
      code: 0xd7,
      label: 'XCHD A,@R1',
      data: 0,
      time: 1,
      callback: _xchdAFromR1),
  OpCode(
      code: 0xd8, label: 'DJNZ R0,REL', data: 1, time: 2, callback: _djnzR0REL),
  OpCode(
      code: 0xd9, label: 'DJNZ R1,REL', data: 1, time: 2, callback: _djnzR1REL),
  OpCode(
      code: 0xda, label: 'DJNZ R2,REL', data: 1, time: 2, callback: _djnzR2REL),
  OpCode(
      code: 0xdb, label: 'DJNZ R3,REL', data: 1, time: 2, callback: _djnzR3REL),
  OpCode(
      code: 0xdc, label: 'DJNZ R4,REL', data: 1, time: 2, callback: _djnzR4REL),
  OpCode(
      code: 0xdd, label: 'DJNZ R5,REL', data: 1, time: 2, callback: _djnzR5REL),
  OpCode(
      code: 0xde, label: 'DJNZ R6,REL', data: 1, time: 2, callback: _djnzR6REL),
  OpCode(
      code: 0xdf, label: 'DJNZ R7,REL', data: 1, time: 2, callback: _djnzR7REL),
  OpCode(
      code: 0xe0,
      label: 'MOVX A,@DPTR',
      data: 0,
      time: 2,
      callback: _moveAFromDPTR),
  OpCode(
      code: 0xe2,
      label: 'MOVX A,@R0',
      data: 0,
      time: 2,
      callback: _moveAFromR0),
  OpCode(
      code: 0xe3,
      label: 'MOVX A,@R1',
      data: 0,
      time: 2,
      callback: _moveAFromR1),
  OpCode(code: 0xe4, label: 'CLR A', data: 0, time: 1, callback: _clrA),
  OpCode(
      code: 0xe5,
      label: 'MOV A,direct',
      data: 1,
      time: 1,
      callback: _moveADirect),
  OpCode(
      code: 0xe6, label: 'MOV A,@R0', data: 0, time: 1, callback: _moveFormAR0),
  OpCode(
      code: 0xe7, label: 'MOV A,@R1', data: 0, time: 1, callback: _moveFormAR1),
  OpCode(code: 0xe8, label: 'MOV A,R0', data: 0, time: 1, callback: _moveAR0),
  OpCode(code: 0xe9, label: 'MOV A,R1', data: 0, time: 1, callback: _moveAR1),
  OpCode(code: 0xea, label: 'MOV A,R2', data: 0, time: 1, callback: _moveAR2),
  OpCode(code: 0xeb, label: 'MOV A,R3', data: 0, time: 1, callback: _moveAR3),
  OpCode(code: 0xec, label: 'MOV A,R4', data: 0, time: 1, callback: _moveAR4),
  OpCode(code: 0xed, label: 'MOV A,R5', data: 0, time: 1, callback: _moveAR5),
  OpCode(code: 0xee, label: 'MOV A,R6', data: 0, time: 1, callback: _moveAR6),
  OpCode(code: 0xef, label: 'MOV A,R7', data: 0, time: 1, callback: _moveAR7),
  OpCode(
      code: 0xf0,
      label: 'MOVX @DPTR,A',
      data: 0,
      time: 2,
      callback: _moveFromDPTRA),
  OpCode(
      code: 0xf2,
      label: 'MOVX @R0,A',
      data: 0,
      time: 2,
      callback: _moveFromR0A),
  OpCode(
      code: 0xf3,
      label: 'MOVX @R1,A',
      data: 0,
      time: 2,
      callback: _moveFromR1A),
  OpCode(code: 0xf4, label: 'CPL A', data: 0, time: 1, callback: _cplA),
  OpCode(
      code: 0xf5,
      label: 'MOV direct,A',
      data: 1,
      time: 1,
      callback: _moveDirectA),
  OpCode(
      code: 0xf6, label: 'MOV @R0,A', data: 0, time: 1, callback: _moveFormR0A),
  OpCode(
      code: 0xf7, label: 'MOV @R1,A', data: 0, time: 1, callback: _moveFormR1A),
  OpCode(code: 0xf8, label: 'MOV R0,A', data: 0, time: 1, callback: _moveR0A),
  OpCode(code: 0xf9, label: 'MOV R1,A', data: 0, time: 1, callback: _moveR1A),
  OpCode(code: 0xfa, label: 'MOV R2,A', data: 0, time: 1, callback: _moveR2A),
  OpCode(code: 0xfb, label: 'MOV R3,A', data: 0, time: 1, callback: _moveR3A),
  OpCode(code: 0xfc, label: 'MOV R4,A', data: 0, time: 1, callback: _moveR4A),
  OpCode(code: 0xfd, label: 'MOV R5,A', data: 0, time: 1, callback: _moveR5A),
  OpCode(code: 0xfe, label: 'MOV R6,A', data: 0, time: 1, callback: _moveR6A),
  OpCode(code: 0xff, label: 'MOV R7,A', data: 0, time: 1, callback: _moveR7A),
];

/// int _sp_max = _sp.def;

///好像只受a寄存器变化影响
_syncPsw({bool? cy, bool? ac, bool? ov}) {
  ///CY
  ///当数据的最高位（D7）进行例如加法操作产生进位时，CY就会置1。
  ///否则CY等于0；当进行8为减法时，若运算结果有借位，则CY=1，否则C=0。
  if (cy != null) cy ? _psw.setBit(0x80) : _psw.clearBit(0x80);

  ///AC
  ///如果低半字节的最高位（D3）有进位，则AC=1，否则AC=0；
  ///当进行8位减法运算时，如果D3有借位，则AC=1，否则AC=0
  if (ac != null) ac ? _psw.setBit(0x40) : _psw.clearBit(0x40);

  ///OV
  ///所以，在加法时，如果最高位（D7）和次高位（D6）中有一个进位，或在减法时两个中有一个借位，OV将被置位。
  ///执行乘法指令（MUL  AB）也会影响OV标志位，当乘积大于255时，OV=1，否则OV=0。
  ///相同为0，不同为1
  ///OV是否置位要同时看最高位和次高位（最高位右边一位）是否有进位或借位，如果同时有或者同时没有，OV=0，一个有另一个没有OV=1

  if (ov != null) ov ? _psw.setBit(0x04) : _psw.clearBit(0x04);

  ///P
  ///每执行一条汇编指令，单片机都能根据A中1的个数的奇偶自动令P置位或清零，奇为1，偶为0
  _acc.isOdd() ? _psw.setBit(0x01) : _psw.clearBit(0x01);
}

_nop(List<int> a, int code, int pc) {}

///累加器的值加程序计数器的值作为其所指定地址，将该地址的内容读入累加器
_movecAAPC(List<int> a, int code, int pc) {
  final t = _acc.value + pc;
  if (t > RomAndRam.getInstance().romSize) {
    Utils.getInstance()
        .abort('can\'t rom access $t over ${RomAndRam.getInstance().romSize} ');
  }
  _acc.value = RomAndRam.getInstance().getCode(t);
  _syncPsw();
}

///A的值加数据指针寄存器的值为其所指定地址，将该地址的内容读入累加器
_movecAADptr(List<int> a, int code, int pc) {
  final t = _acc.value + _dph.value * 0x100 + _dpl.value;
  if (t > RomAndRam.getInstance().romSize) {
    Utils.getInstance()
        .abort('can\'t rom access $t over ${RomAndRam.getInstance().romSize} ');
  }
  _acc.value = RomAndRam.getInstance().getCode(t);
  _syncPsw();
}

///将16位的常数存入数据指针寄存器
_moveDptrData16(List<int> a, int code, int pc) {
  _dph.value = a.first;
  _dpl.value = a.last;
}

///将常数存入直接地址
_moveDirectData(List<int> a, int code, int pc) {
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = a.last;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = a.last;
  }
}

///将直接地址2的内容存入直接地址1 858089  MOV aac(89),aa(80)  aa->aac
_moveDirectDirect(List<int> a, int code, int pc) {
  if (a.first >= 0x80) {
    try {
      final s = _getRegisiter(a.first);
      if (a.last >= 0x80) {
        final d = _getRegisiter(a.last);
        d.value = s.value;
      } else {
        _iData[a.last] = s.value;
      }
    } catch (e) {
      Utils.getInstance().abort(
          'can\'t find regisiter(${a.first.toHexString(8)}:${a.last.toHexString(8)})');
    }
  } else {
    if (a.last >= 0x80) {
      try {
        final d = _getRegisiter(a.last);
        d.value = _iData[a.first];
      } catch (e) {
        Utils.getInstance()
            .abort('can\'t find regisiter(${a.last.toHexString(8)})');
      }
    } else {
      _iData[a.last] = _iData[a.first];
    }
  }
}

///将寄存器的内容存入直接地址
_moveDirectR0(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8];
  }
}

_moveDirectR1(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 1];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 1];
  }
}

_moveDirectR2(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 2];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 2];
  }
}

_moveDirectR3(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 3];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 3];
  }
}

_moveDirectR4(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 4];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 4];
  }
}

_moveDirectR5(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 5];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 5];
  }
}

_moveDirectR6(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 6];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 6];
  }
}

_moveDirectR7(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[m * 8 + 7];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[m * 8 + 7];
  }
}

///将间接地址的内容存入直接地址
_moveDirectFromR0(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[_iData[m * 8]];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[_iData[m * 8]];
  }
}

_moveDirectFromR1(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _iData[_iData[m * 8 + 1]];
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _iData[_iData[m * 8 + 1]];
  }
}

///将累加器的内容存入直接地址
_moveDirectA(List<int> a, int code, int pc) {
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      t.value = _acc.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[a.first] = _acc.value;
  }
}

///将常数载入寄存器
_moveR0Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8] = a.first;
}

_moveR1Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 1] = a.first;
}

_moveR2Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 2] = a.first;
}

_moveR3Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 3] = a.first;
}

_moveR4Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 4] = a.first;
}

_moveR5Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 5] = a.first;
}

_moveR6Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 6] = a.first;
}

_moveR7Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 7] = a.first;
}

///将直接地址的内容载入寄存器
_moveR0Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8] = _iData[a.first];
  }
}

_moveR1Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 1] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 1] = _iData[a.first];
  }
}

_moveR2Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 2] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 2] = _iData[a.first];
  }
}

_moveR3Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 3] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 3] = _iData[a.first];
  }
}

_moveR4Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 4] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 4] = _iData[a.first];
  }
}

_moveR5Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 5] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 5] = _iData[a.first];
  }
}

_moveR6Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 6] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 6] = _iData[a.first];
  }
}

_moveR7Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[m * 8 + 7] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[m * 8 + 7] = _iData[a.first];
  }
}

///将常数存入某间接地址
_moveFormR0Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8];
  _iData[t] = a.first;
}

_moveFormR1Data(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8 + 1];
  _iData[t] = a.first;
}

///将直接地址的内容存入某间接地址
_moveFormR0Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[_iData[m * 8]] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[_iData[m * 8]] = _iData[a.first];
  }
}

_moveFormR1Direct(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _iData[_iData[m * 8 + 1]] = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _iData[_iData[m * 8 + 1]] = _iData[a.first];
  }
}

///将累加器的内容存入某间接地址
_moveFormR0A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8];
  _iData[t] = _acc.value;
}

_moveFormR1A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8 + 1];
  _iData[t] = _acc.value;
}

///将累加器的内容载入寄存器
_moveR0A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8] = _acc.value;
}

_moveR1A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 1] = _acc.value;
}

_moveR2A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 2] = _acc.value;
}

_moveR3A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 3] = _acc.value;
}

_moveR4A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 4] = _acc.value;
}

_moveR5A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 5] = _acc.value;
}

_moveR6A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 6] = _acc.value;
}

_moveR7A(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  _iData[m * 8 + 6] = _acc.value;
}

///将常数载入累加器
_moveAData(List<int> a, int code, int pc) {
  _acc.value = a.first;
  _syncPsw();
}

///将直接地址的内容载入累加器
_moveADirect(List<int> a, int code, int pc) {
  if (a.first >= 0x80) {
    try {
      final t = _getRegisiter(a.first);
      _acc.value = t.value;
    } catch (e) {
      Utils.getInstance()
          .abort('can\'t find regisiter(${a.first.toHexString(8)})');
    }
  } else {
    _acc.value = _iData[a.first];
  }
  _syncPsw();
}

///将间接地址的内容载入累加器
_moveFormAR0(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8];
  _acc.value = _iData[t];
  _syncPsw();
}

_moveFormAR1(List<int> a, int code, int pc) {
  final m = _psw.mask(0x18) >> 3;
  final t = _iData[m * 8 + 1];
  _acc.value = _iData[t];
  _syncPsw();
}

///将寄存器的内容载入累加器
_moveAR0(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8];
  _syncPsw();
}

_moveAR1(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 1];
  _syncPsw();
}

_moveAR2(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 2];
  _syncPsw();
}

_moveAR3(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 3];
  _syncPsw();
}

_moveAR4(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 4];
  _syncPsw();
}

_moveAR5(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 5];
  _syncPsw();
}

_moveAR6(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 6];
  _syncPsw();
}

_moveAR7(List<int> a, int code, int pc) {
  final t = _psw.mask(0x18) >> 3;
  _acc.value = _iData[t * 8 + 7];
  _syncPsw();
  // final t = _psw.mask(0x18);
  // int tmp = _acc.value;
  // bool ac = false;
  // if (t == 0) {
  //   ac = (tmp & 0x0f) + (_r07.value & 0x0f) > 0x0f;
  //   tmp += _r00.value;
  // } else if (t == 1) {
  //   ac = (tmp & 0x0f) + (_r17.value & 0x0f) > 0x0f;
  //   tmp += _r10.value;
  // } else if (t == 2) {
  //   ac = (tmp & 0x0f) + (_r27.value & 0x0f) > 0x0f;
  //   tmp += _r20.value;
  // } else if (t == 3) {
  //   ac = (tmp & 0x0f) + (_r37.value & 0x0f) > 0x0f;
  //   tmp += _r30.value;
  // } else {
  //   Utils.getInstance().abort('get error psw status:RS[$t]');
  // }
  // int ov = (_acc.value & 0xc0) ^ (tmp & 0xc0);
  // _acc.value = tmp & 0xff;
  // _syncPsw(cy: tmp > 0xff, ac: ac, ov: ov != 0 && ov != 0xc0);
}

///内部256b ram
///data：片内 RAM 从 0x00~0x7F 直接寻址
///_iData：片内 RAM 从 0x00~0xFF 通用寄存器间接寻址
///pdata：片外 RAM 从 0x00~0xFF 通用寄存器间接寻址
//xdata：片外 RAM 从 0x0000~0xFFFF 通用寄存器间接寻址
List<int> _iData = List.generate(256, (index) => 0, growable: false);

// final _r00 = Regisiter(addr: 0x00, label: "R0", def: 0, isGeneral: true);
// final _r01 = Regisiter(addr: 0x01, label: "R1", def: 0, isGeneral: true);
// final _r02 = Regisiter(addr: 0x02, label: "R2", def: 0, isGeneral: true);
// final _r03 = Regisiter(addr: 0x03, label: "R3", def: 0, isGeneral: true);
// final _r04 = Regisiter(addr: 0x04, label: "R4", def: 0, isGeneral: true);
// final _r05 = Regisiter(addr: 0x05, label: "R5", def: 0, isGeneral: true);
// final _r06 = Regisiter(addr: 0x06, label: "R6", def: 0, isGeneral: true);
// final _r07 = Regisiter(addr: 0x07, label: "R7", def: 0, isGeneral: true);
// final _r10 = Regisiter(addr: 0x08, label: "R0", def: 0, isGeneral: true);
// final _r11 = Regisiter(addr: 0x09, label: "R1", def: 0, isGeneral: true);
// final _r12 = Regisiter(addr: 0x0a, label: "R2", def: 0, isGeneral: true);
// final _r13 = Regisiter(addr: 0x0b, label: "R3", def: 0, isGeneral: true);
// final _r14 = Regisiter(addr: 0x0c, label: "R4", def: 0, isGeneral: true);
// final _r15 = Regisiter(addr: 0x0d, label: "R5", def: 0, isGeneral: true);
// final _r16 = Regisiter(addr: 0x0e, label: "R6", def: 0, isGeneral: true);
// final _r17 = Regisiter(addr: 0x0f, label: "R7", def: 0, isGeneral: true);
// final _r20 = Regisiter(addr: 0x10, label: "R0", def: 0, isGeneral: true);
// final _r21 = Regisiter(addr: 0x11, label: "R1", def: 0, isGeneral: true);
// final _r22 = Regisiter(addr: 0x12, label: "R2", def: 0, isGeneral: true);
// final _r23 = Regisiter(addr: 0x13, label: "R3", def: 0, isGeneral: true);
// final _r24 = Regisiter(addr: 0x14, label: "R4", def: 0, isGeneral: true);
// final _r25 = Regisiter(addr: 0x15, label: "R5", def: 0, isGeneral: true);
// final _r26 = Regisiter(addr: 0x16, label: "R6", def: 0, isGeneral: true);
// final _r27 = Regisiter(addr: 0x17, label: "R7", def: 0, isGeneral: true);
// final _r30 = Regisiter(addr: 0x18, label: "R0", def: 0, isGeneral: true);
// final _r31 = Regisiter(addr: 0x19, label: "R1", def: 0, isGeneral: true);
// final _r32 = Regisiter(addr: 0x1a, label: "R2", def: 0, isGeneral: true);
// final _r33 = Regisiter(addr: 0x1b, label: "R3", def: 0, isGeneral: true);
// final _r34 = Regisiter(addr: 0x1c, label: "R4", def: 0, isGeneral: true);
// final _r35 = Regisiter(addr: 0x1d, label: "R5", def: 0, isGeneral: true);
// final _r36 = Regisiter(addr: 0x1e, label: "R6", def: 0, isGeneral: true);
// final _r37 = Regisiter(addr: 0x1f, label: "R7", def: 0, isGeneral: true);

final _b = Regisiter(addr: 0xf0, label: "B", def: 0, isGeneral: true);
final _acc = Regisiter(addr: 0xe0, label: "A/ACC", def: 0, isGeneral: true);
final _psw = Regisiter(addr: 0xd0, label: "PSW", def: 0, isGeneral: true);
final _dph = Regisiter(addr: 0x83, label: "DPH", def: 0, isGeneral: true);
final _dpl = Regisiter(addr: 0x82, label: "DPL", def: 0, isGeneral: true);
final _sp = Regisiter(addr: 0x81, label: "SP", def: 0x07, isGeneral: true);

Regisiter _getRegisiter(int addr) {
  for (var element in mcs51Module) {
    try {
      return element.registers.firstWhere((e) => e.addr == addr);
    } catch (e) {
      {}
    }
  }
  throw addr;
}

///MCS52为256B  MCS51是128B
///00H — 7FH ( 0 —127 )单元组成的128字节的RAM块
///80H ---- FFH ( 128 — 255 )单元组成的 128字节的RAM块 （MCS-51中没有，这一个部分）
///80H ---- FFH ( 128 — 255 ）的 SFR 特殊功能寄存器块
///在MCS-52 中，高128字节的RAM 和SFR块地址重叠，但是物理上是两个不同的地方。
///访问的时候，根据指令的不同，来确定是访问RAM，还是SFR。
///访问高80H ---- FFH RAM是 间接寻址方式。
///访问SFR 是 直接寻址方式。
///低128字节的RAM 间接 直接都可以。
///
///SFR 这128个字节中仅仅有26个字节是有意义的，这26个是标准定义的寄存器,厂商会增加或修改。
///暂时无法处理分页寄存器
///默认导出的模块
final List<Module> mcs51Module = List.from(<Module>[
  MCS51CPUModule(name: 'CPU', registers: [
    _b,
    _acc,
    _psw,
    _dph,
    _dpl,
    _sp,
  ]),
  ExamModule(name: 'EXAM', registers: [Regisiter(addr: 0x80)]),
  GpioModule(name: 'GPIO', sync_: false, registers: []),
]);

///中断还是得经过查询来处理,好处理嵌套什么的
void mcs51InterruptHandle() {}
